//
//  XKMineHeaderCell.m
//  XKJY
//
//  Created by xuyaguang on 2017/1/12.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKMineHeaderCell.h"


@interface XKMineHeaderCell ()

@property (nonatomic, strong) UIImageView   *iconImageView;         // 头像
@property (nonatomic, strong) UILabel       *levelNameLabel;        // 用户等级图标
@property (nonatomic, strong) UILabel       *nickNameLabel;         // 昵称
@property (nonatomic, strong) UIButton      *wosLevelupButton;      // 种子升级按钮
@property (nonatomic, strong) UIButton      *fastLevelupButton;     // 快速升级按钮

@end
@implementation XKMineHeaderCell

#pragma mark - 自定义cell重写构造方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configArguments];
        [self createSubviews];
    }
    return self;
}



#pragma mark - 设置自身
- (void)configArguments {
    self.backgroundColor = kBaseRedColor;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}


#pragma mark - 用户升级会员等级
- (void)wosLevelupButtonClick:(UIButton *)sender {
    if (self.levelUpBlock) {
        self.levelUpBlock();
    }
}

#pragma mark - 快速升级会员等级
- (void)fastwosLevelupButtonClick:(UIButton *)sender {
    if (self.fastLevelUpBlock) {
        self.fastLevelUpBlock();
    }
}

#pragma mark - 点击等级说明文字
- (void)levelNameLabelTapEvent:(UITapGestureRecognizer *)tap {
    NSString *userLevel = [UserDefaults objectWithKey:@"level"];
    if (![userLevel isEqualToString:@"4"]){
        if (self.levelNameLabelBlock) {
            self.levelNameLabelBlock();
        }
    }
}

#pragma mark - 创建控件
- (void)createSubviews {
    
    _iconImageView = [UIImageView new];
    _iconImageView.backgroundColor = [UIColor whiteColor];
    _iconImageView.layer.cornerRadius = 34*Scale_W;
    _iconImageView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.iconImageView];
    
    _levelNameLabel = [UILabel getLabWithText:@">>小康100<<"
                                     textFont:Font_17
                                    textColor:YellowColor
                                textAlignment:NSTextAlignmentCenter];
    [self.contentView addSubview:self.levelNameLabel];
    
    self.levelNameLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(levelNameLabelTapEvent:)];
    [self.levelNameLabel addGestureRecognizer:tap];
    
    _nickNameLabel = [UILabel getLabWithText:@"xiaokang100"
                                    textFont:Font_15
                                   textColor:WhiteColor
                               textAlignment:NSTextAlignmentCenter];
    [self.contentView addSubview:self.nickNameLabel];
    
    _wosLevelupButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _wosLevelupButton.titleLabel.font = Font(15);
    [_wosLevelupButton setBackgroundColor:UIColorFromRGB(0xe94c4e)];
    [_wosLevelupButton setTitle:@"种子升级" forState:UIControlStateNormal];
    [_wosLevelupButton setTitleColor:UIColorFromRGB(0xc9d3d3)
                            forState:UIControlStateNormal];
    [_wosLevelupButton addTarget:self
                          action:@selector(wosLevelupButtonClick:)
                forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.wosLevelupButton];
    
    
    _fastLevelupButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _fastLevelupButton.titleLabel.font = Font(15);
    [_fastLevelupButton setBackgroundColor:UIColorFromRGB(0xe94c4e)];
    [_fastLevelupButton setTitle:@"快速升级" forState:UIControlStateNormal];
    [_fastLevelupButton setTitleColor:UIColorFromRGB(0xc9d3d3)
                             forState:UIControlStateNormal];
    [_fastLevelupButton addTarget:self
                           action:@selector(fastwosLevelupButtonClick:)
                 forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_fastLevelupButton];
    
    [self addConstraints];
    
}
#pragma mark - 添加约束
- (void)addConstraints {
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(60*Scale_W);
        make.centerX.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(68*Scale_W, 68*Scale_W));
    }];
    
    [self.levelNameLabel  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(8*Scale_W);
        make.centerX.mas_equalTo(self.iconImageView);
    }];
    
    
    NSString *userLevel = [UserDefaults objectWithKey:@"level"];
    if ([userLevel isEqualToString:@"4"]) {
        [self.nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.levelNameLabel.mas_bottom).offset(8*Scale_W);
            make.centerX.mas_equalTo(self.levelNameLabel);
            make.bottom.mas_equalTo(self.contentView).offset(-20*Scale_W).priorityHigh();
        }];
    } else {
        [self.nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.levelNameLabel.mas_bottom).offset(8*Scale_W);
            make.centerX.mas_equalTo(self.levelNameLabel);
        }];
        
        [self.wosLevelupButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.nickNameLabel.mas_bottom).offset(8*Scale_W);
            make.left.mas_equalTo(self.contentView);
            make.height.mas_equalTo(40*Scale_W);
            make.right.mas_equalTo(self.contentView.mas_centerX).offset(-0.5);
            make.bottom.mas_equalTo(self.contentView).priorityHigh();
        }];
        
        [self.fastLevelupButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.bottom.mas_equalTo(self.contentView);
            make.height.mas_equalTo(self.wosLevelupButton);
            make.left.mas_equalTo(self.contentView.mas_centerX).offset(0.5);
        }];
    }
}


- (void)setNickString:(NSString *)nickString {
    _nickString = nickString;
    if (nickString.length == 0) {
        self.nickNameLabel.text = [UserDefaults objectWithKey:@"phone"];
        return;
    }
    self.nickNameLabel.text = nickString;
}

- (void)setIconString:(NSString *)iconString {
    _iconString = iconString;
}

- (void)setUserLevelString:(NSString *)userLevelString {
   
    
    [self.iconImageView sd_setImageWithURL:nil
                          placeholderImage:[UIImage imageNamed:[NSString stringWithFormat:@"app_icon_placeholder_%@",userLevelString]]];
    
    switch (userLevelString.integerValue) {
        case 1:self.levelNameLabel.text = @">>商城会员<<"; break;
        case 2:self.levelNameLabel.text = @">>小康会员<<"; break;
        case 3:self.levelNameLabel.text = @">>消费商<<"; break;
        case 4:self.levelNameLabel.text = @">>金牌消费商<<"; [self remakeConstraints];break;
        default:self.levelNameLabel.text = @">>未知等级<<";break;
    }
}


// 隐藏升级按钮
- (void)remakeConstraints {
    
        [self.nickNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.levelNameLabel.mas_bottom).offset(8*Scale_W);
            make.centerX.mas_equalTo(self.levelNameLabel);
            make.bottom.mas_equalTo(self.contentView).offset(-20*Scale_W).priorityHigh();
        }];
    
        [self.wosLevelupButton removeFromSuperview];
        [self.fastLevelupButton removeFromSuperview];
    
}



@end
